#include <sys/types.h>          /* See NOTES */
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <net/if.h>

#include "proto.h"

int main(void)
{
    struct sockaddr_in raddr;
    struct ip_mreqn imr;
    int udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
    char msg[] = "from ice msg:hello";

    if (-1 == udp_socket) {
        perror("socket()");
        return 1;
    }

    inet_aton(GROUP_ADDR, &imr.imr_multiaddr);
    inet_aton("0.0.0.0", &imr.imr_address);
    imr.imr_ifindex = if_nametoindex("ens37");

    if (-1 == setsockopt(udp_socket, IPPROTO_IP, IP_MULTICAST_IF, &imr, sizeof(imr))) {
        perror("setsockopt()");
        close(udp_socket);
        return 1;
    }

    raddr.sin_family = AF_INET;
    inet_aton(GROUP_ADDR, &raddr.sin_addr);
    raddr.sin_port = htons(RCV_PORT);

    while (1) {
        sendto(udp_socket, msg, strlen(msg) + 1, 0, (struct sockaddr *)&raddr, sizeof(raddr));

        sleep(1);
    }
    
    close(udp_socket);

    return 0;
}

